package com.huanwei.mainfragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.mainfragment.adapter.Speach_Adapter;
import com.huanwei.cc.bean.Paim_Bean;
import com.huanwei.cc.activity.Edit2NoteActivity;
import com.huanwei.map.AMapUtil;

import java.util.Locale;

import butterknife.ButterKnife;

/**
 * Created by zhuwenhai on 2017/12/1.
 * 语音播放
 */

public class Speech3_Fragment extends Fragment {

    View view;
    Speach_Adapter bank_card_adapter;
    TextToSpeech textToSpeech;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_main2, null);
            ButterKnife.bind(this, view);
            initViewsAndEvents();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }


    protected void initViewsAndEvents() {
        textToSpeech = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(getActivity(), "TTS暂时不支持这种语音的朗读！",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        ListView lvList = (ListView) view.findViewById(R.id.lv_list);//设置;
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), Edit2NoteActivity.class));

            }
        });
        bank_card_adapter = new Speach_Adapter();
        lvList.setAdapter(bank_card_adapter);
        bank_card_adapter.addItem(new Paim_Bean("樱花大道", "您即将去的地方是南京林业大学樱花大道，南京林业大学玄武湖校区景色素以樱花最出名，每年的三、四月份，南林大老图书馆前面的一条几十米的小路上，樱花树上都会开满樱花。放眼望去，大片白色的樱花在微风的吹拂之下漫天飞舞，美不胜收。樱花常常在一夜之间，迅速开放，突如其来，势不可挡。盛开过后，便默默凋谢，没有任何留恋。","118.4833","32.447"));
        bank_card_adapter.addItem(new Paim_Bean("老图书馆", "南京林业大学图书馆创建于1952年，藏书170万余册。1961年建成图书馆大楼，面积7600平方米；1996年又建成新馆，面积13000平方米。新旧馆通过外廊相连。新馆主要为图书借还、图书阅览、电子阅览等，旧馆主要是报刊阅览、信息咨询、行政办公和书库等。 此外，还有生命科学等三个分馆和阅览室，五个规格不等的报告厅。","118.4832","32.449"));
        bank_card_adapter.addItem(new Paim_Bean("新图书馆", "能听音乐看电影，有苹果电脑、新媒体体验，多功能空间家具，高大上的图书馆标识系统，明亮温馨的环境、整洁的大厅，这个充满科技和文艺气息的地方在哪里？没错，如此高逼格的地方！就是南京林业大学的新图书馆！","118.4844","32.445"));
        bank_card_adapter.addItem(new Paim_Bean("体育馆", "南京林业大学体育馆是一所综合性多功能场馆，里面有篮球馆，乒乓球馆，健身房，排球馆。还有承接一些大型活动，大型会议，演出，招聘会等。","118.4843","32.456"));
        bank_card_adapter.addItem(new Paim_Bean("教九综合院楼", "教学九楼位于学校南大山地块，主要为教学、实验用房，建筑面积39938.62平方米，地上6层，地下1层，建筑高度28.55米。同时也是轻工与食品学院，汽车与交通工程学院，理学院，风景园林学院的院楼。","118.491","32.448"));


        bank_card_adapter.notifyDataSetChanged();
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Paim_Bean item = (Paim_Bean) bank_card_adapter.getItem(i);
                showDialog(item.getWeidu(), item.getJingdu(), item.getName());
            }
        });
    }


    private void showDialog(final String Latitude, final String Longitude, final String neirong) {
  /*
  这里使用了 android.support.v7.app.AlertDialog.Builder
  可以直接在头部写 import android.support.v7.app.AlertDialog
  那么下面就可以写成 AlertDialog.Builder
  */
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("请选择到目的地或者语音播报？");
        builder.setNegativeButton("语音播报", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (textToSpeech.isSpeaking()) {
                    textToSpeech.stop();
                }

                textToSpeech.speak(neirong,
                        TextToSpeech.QUEUE_ADD, null);
            }
        });
        builder.setPositiveButton("去目的地", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (AMapUtil.isInstallByRead("com.autonavi.minimap")) {

                    AMapUtil.goToNaviActivity(getActivity(), "test", null, String.valueOf(Latitude), String.valueOf(Longitude), "1", "2");
                } else {
                    Toast.makeText(getActivity(), "请安装高德地图", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.show();
    }

    @Override
    public void onDestroy() {
        if (textToSpeech != null)
            textToSpeech.shutdown();
        super.onDestroy();
    }

}
